local skel = fk.CreateSkill{--定义
  name = "kt_st__fubai",
}

Fk:loadTranslationTable{--翻译
  ["kt_st__fubai"] = "复拜",
  [":kt_st__fubai"] = "结束阶段，你可以与任意名同势力角色依次交换一张牌。你可以将此技能交给主公作为主公技并视为使用一张基本牌，其使用同名牌后移回。",
  ["kt_st__fubaiload"] = "复拜",
  [":kt_st__fubaiload"] = "主公技，结束阶段，你可以与任意名同势力角色依次交换一张牌。你使用同名牌后移回此技能。",
  ["#kt_st__fubai1_ask"] = "复拜：你可以与任意名同势力角色依次交换一张牌",
  ["#kt_st__fubai2_ask"] = "交换你与其的一张牌",
  ["#kt_st__fubai3_ask"] = "复拜：你可以将此技能交给主公作为主公技并视为使用一张基本牌",
  ["@kt_st__fubai"] = "复拜",

  ["$kt_st__fubai1"] = "既然称变，则千里福地，亦为君土，为君臣。",
  ["$kt_st__fubai2"] = "当涂者以大利所驱，莫非——欲为圣人乎？",

}

---@param player Player
---@return Player|nil
local function getload(player)
  if player.role == "lord" then
    return nil
  end
  local loadp = player.next
  while loadp.role ~= "lord" and loadp ~= player do
    loadp = loadp.next
  end
  if loadp and loadp.role == "lord" and loadp.dead == false then
    return loadp
  else
    return nil
  end
end

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic",
  prompt = "#kt_st__fubai3_ask",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(skel.name, all_names),
      all_choices = all_names,
    }
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local to = room:getLord()
    if not to or to.dead then return end
    room:handleAddLoseSkills(player,"-"..skel.name)
    room:handleAddLoseSkills(to,"kt_st__fubaiload")
    room:setPlayerMark(to,"@kt_st__fubai",use.card.trueName)
    room:setPlayerMark(to,"kt_st__fubai2",player.id)
    room:doIndicate(player, {to})
  end,
  enabled_at_play = function(self, player)
    return player:hasSkill(skel.name) and getload(player) and not getload(player):hasSkill("kt_st__fubaiload") and
      #player:getViewAsCardNames(skel.name, Fk:getAllCardNames("b")) > 0
  end,
  enabled_at_response = function(self, player, response)
    if response or not player:hasSkill(skel.name) then return end
    return getload(player) and not getload(player):hasSkill("kt_st__fubaiload") and
      #player:getViewAsCardNames(skel.name, Fk:getAllCardNames("b")) > 0
  end,
})


Fk:addPoxiMethod{
  name = "kt_st__fubai2_ask",
  prompt = "#kt_st__fubai2_ask",
  card_filter = function(to_select, selected, data)
    if #selected < 2 then
      if #selected == 0 then
        return true
      else
        if table.contains(data[1][2], selected[1]) then
          return table.contains(data[2][2], to_select)
        else
          return table.contains(data[1][2], to_select)
        end
      end
    end
  end,
  feasible = function(selected)
    return #selected == 2
  end,
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    local list = table.filter(player.room:getOtherPlayers(player),function (p)
      return p.kingdom == player.kingdom and not p:isNude()
    end)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
    and not player:isNude() and #list > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local list = table.filter(room:getOtherPlayers(player),function (p)
      return p.kingdom == player.kingdom and not p:isNude()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 999,
      targets = list,
      skill_name = skel.name,
      prompt = "#kt_st__fubai1_ask",
      cancelable = true,
      --no_indicate = true
    })
    if #to > 0 then
      event:setCostData(self, {extra_data = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local players = event:getCostData(self).extra_data
    room:doIndicate(player, {players})
    for _,p in ipairs(players) do
      if player:isNude() or p:isNude() then return end
      --
      local result = room:askToPoxi(player, {
        poxi_type = "kt_st__fubai2_ask",
        data = {
          { player.general, player:getCardIds("he") },
          { p.general, p:getCardIds("he") },
        },
        cancelable = false,
      })
      if #result ~= 2 then return end
      local id1, id2 = result[1], result[2]
      if table.contains(p:getCardIds("he"), result[2]) then
        id1, id2 = result[2], result[1]
      end
      local moves = {}
      table.insert(moves, {
        ids = {id1},
        from = p,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = skel.name,
        proposer = player,
        moveVisible = false,
        --drawPilePosition = table.indexOf(cards, id2),
      })
      table.insert(moves, {
        ids = {id2},
        from = player,
        to = p,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        skillName = skel.name,
        proposer = player,
        moveVisible = false,
      })
      room:moveCards(table.unpack(moves))
      --
    end
  end,
})

return skel